package packets;

import org.jnetpcap.packet.JHeader;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.annotate.Bind;
import org.jnetpcap.packet.annotate.Field;
import org.jnetpcap.packet.annotate.Header;
import org.jnetpcap.packet.annotate.FlowKey;
import org.jnetpcap.protocol.network.Ip4;



@Header(length = 24, name = "OSPF", description = "OSPF Header")
public class OSPF extends JHeader {


	
public enum OSPFType{
	OSPFHello(0x01, "OSPF Hello"),
	OSPFDescription(0x02, "OSPF Database Description"),
	OSPFRequest(0x03, "OSPF Link State Request"),
	OSPFUpdate(0x04, "OSPF Link State Update"),
	OSPFAck(0x05, "OSPF Link State Acknowledgement");
	
	private final String description;
	private final int id;
	
	public static OSPFType valueOf(int type){
		for (OSPFType t: values()){
			if(t.id == type){
				return t;
			}
		}
		return null;
	}
	
	private OSPFType(int id){
		this.id = id;
		this.description = name().toLowerCase();
	}
	
	
	
	private OSPFType(int id, String description){
		this.id = id;
		this.description = description;
	}
	
	

}


@Bind(to = Ip4.class)
public static boolean bindToIp4(JPacket packet, Ip4 ip){
	return ip.type() == 89;
}


@Field(offset = 0, length = 1 * 8)
public int version(){
	return getUByte(0);
}

@Field(offset = 1 * 8, length = 1 * 8)
public int type(){
	return getUByte(1);
}

@Field(offset = 2*8, length = 2 * 8)
public byte[] packetLength(){
	return getByteArray(2, 2);
}

@Field(offset = 4*8, length = 4*8)
public byte[] routerID(){
	return getByteArray(4, 4);
}

@Field(offset = 8*8, length = 4*8)
public byte[] areaID(){
	return getByteArray(8, 4);
}

@Field(offset = 12 * 8, length = 2*8)
public byte[] checksum(){
	return getByteArray(12, 2);
}

@Field(offset = 14 * 8, length = 2*8)
public int auType(){
	return getUShort(14);
}

@Field(offset = 16 * 8, length = 8*8)
public byte[] authenticationField(){
	return getByteArray(16, 8);
}
	
}
